package cn.itsource.ymcc.mq.consumer;

import cn.itsource.ymcc.dto.Order2PayParamDto;
import cn.itsource.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup="service-courser-order-consumer",//消费者组
        topic="order-topic",//topic
        selectorExpression="payorder-tags",//tags
        messageModel= MessageModel.CLUSTERING//集群模式，一个消息只会给到一个消费者
        //还有一个广播模式，一个消息可以被多个消费者消费
)
public class ConsumerCourseOrderPayMessage implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 消费者只能成功消费一次
     * 这个方法只要不报错，就是自动签收
     *
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        Order2PayParamDto order2PayParamDto = JSONObject.parseObject(message, Order2PayParamDto.class);
        payOrderService.savePayOrder(order2PayParamDto);
    }
}
